<template>
    <div class="p-2">
        <el-card shadow="never">
            <template #header>
                <el-row justify="space-between">
                    <el-col :span="1.5">
                        <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                            <el-form-item label="申请日期：" prop="timeDate">
                                <el-date-picker v-model="queryParams.timeDate" type="daterange" range-separator="至"
                                    start-placeholder="开始时间" end-placeholder="结束时间" @change="handleTimeChange"
                                    format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                            </el-form-item>
                            <el-form-item label="审批状态：" prop="orderPrintStatus">
                                <el-select v-model="queryParams.orderPrintStatus" clearable>
                                    <el-option label="未打印" value="0"></el-option>
                                    <el-option label="已打印" value="1"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="模糊查询：" prop="workName">
                                <el-input v-model="queryParams.workName" placeholder="请输入"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button icon="DocumentDelete" @click="exportPurchaseList" type="success">下载EXCEL</el-button>
                    </el-col>
                </el-row>
            </template>
            <el-table v-loading="loading" :data="dataList" stripe>
                <el-table-column label="所属公司" prop="companyName" align="center"></el-table-column>
                <el-table-column label="申请人" prop="applyName" align="center"></el-table-column>
                <el-table-column label="申请日期" prop="applyDate" align="center"></el-table-column>
                <el-table-column label="入库日期" prop="recentInDate" align="center"></el-table-column>
                <el-table-column label="入库编号" prop="purchaseInRecordId" align="center"></el-table-column>
                <el-table-column label="材料小类" prop="productSubcategoryName" align="center"></el-table-column>
                <el-table-column label="规格名称" prop="specsDetail" align="center"></el-table-column>
                <el-table-column label="申请数量" prop="number" align="center"></el-table-column>
                <el-table-column label="采购数量" prop="purchaseNumber" align="center"></el-table-column>
                <el-table-column label="入库数量" prop="inNumber" align="center"></el-table-column>
                <el-table-column label="单价" prop="purchasePrice" align="center"></el-table-column>
                <el-table-column label="入库金额" align="center">
                    <template #default="scope">
                        {{ Number(scope.row.inNumber) * Number(scope.row.purchasePrice) }}
                    </template>
                </el-table-column>
                <el-table-column label="供应商" prop="supplyFactoryName" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center">
                    <template #default="scope">
                        <el-button @click="handleViewNotes(scope.row)" type="primary" link>查看备注</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="对账状态" prop="updateTime" align="center">
                    <template #default="scope">
                        <span style="color: #F98075;" v-if="scope.row.reconciliationFlag == '0'">未对账</span>
                        <span style="color: #E6A33F;" v-if="scope.row.reconciliationFlag == '1'">已对账</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="240px">
                    <template #default="scope">
                        <el-tooltip content="申请对账" placement="top">
                            <el-button @click="handleOpenApply(scope.row)" link :style="{ fontSize: '13px' }"
                                type="success" icon="Money"></el-button>
                        </el-tooltip>
                        <el-tooltip content="撤销申请对账">
                            <el-button @click="handleRevokeApply(scope.row, getList)" link :style="{ fontSize: '13px' }"
                                type="danger" icon="RefreshLeft"></el-button>
                        </el-tooltip>
                        <el-tooltip content="撤销入库" placement="top">
                            <el-button @click="handleOpenRevokeStorage(scope.row)" link :style="{ fontSize: '13px' }"
                                type="danger" icon="RefreshRight"></el-button>
                        </el-tooltip>
                        <el-tooltip content="详情">
                            <el-button @click="handleOpenDetail(scope.row)" link :style="{ fontSize: '13px' }"
                                type="primary" icon="Document"></el-button>
                        </el-tooltip>
                        <el-tooltip content="查看验收报告" placement="top">
                            <el-button @click="handleOpenReport(scope.row)" link :style="{ fontSize: '13px' }"
                                type="success" icon="FolderChecked"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>

            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 撤销入库弹窗 -->
        <RevokeStorage :visible="revokeStorageVisible" :initList="revokeStorageObj" @submit="getList" @close="
            revokeStorageVisible = false" />
        <!-- 查看验收报告 -->
        <AcceptanceReport :visible="reportVisible" :initList="reportRow" :dataList="reportObj"
            @close="reportVisible = false" />
        <!-- 申请对账 -->
        <ApplyForReconciliation :visible="applyVisible" :initList="applyObj" @submit="getList"
            @close="applyVisible = false" />
        <!-- 采购入库详情 -->
        <!-- <WarehousingDetails :visible="detailVisible" @close="detailVisible = false" /> -->
        <!-- 查看备注弹窗 -->
        <ViewNotes :visible="viewNotesVisible" :initList="viewNotesObj" @close="viewNotesVisible = false" />

        <!-- 查看详情 -->
        <OrderDetails :visible="detailVisible" :initList="detailRow" :dataList="detailObj"
            @close="detailVisible = false" />

    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import { getPurchasedOrdersListApi } from '@/api/sj/purchaseManagement/purchaseOrder/index';
import { getFormattedTimestamp } from '@/utils/dateFormat';

import OrderDetails from '@/views/purchaseManagement/toBeStored/components/details.vue';
/** 采购入库详情 */
// import WarehousingDetails from './components/warehousingDetails.vue';
// import useWarehousingDetails from '@/hooks/purchaseManagement/purchaseOrder/useWarehousingDetails';
// const { detailVisible, handleOpenDetails } = useWarehousingDetails();

/** 撤销入库 */
import RevokeStorage from './components/revokeStorage.vue';
import useRevokeStorage from '@/hooks/purchaseManagement/purchaseOrder/useRevokeStorage';

/** 验收报告 */
import AcceptanceReport from './components/acceptanceReport.vue';
import useAcceptanceReport from '@/hooks/purchaseManagement/purchaseOrder/useAcceptanceReport';

/** 申请对账 */
import ApplyForReconciliation from './components/applyForReconciliation.vue';
import useApplyForReconciliation from '@/hooks/purchaseManagement/toBeStored/useApplyForReconciliation';

/** 查看备注 */
import ViewNotes from '@/views/purchaseManagement/toBeStored/components/viewNotes.vue';
import useViewNotes from '@/hooks/purchaseManagement/toBeStored/useViewNotes';
const { viewNotesVisible, detailVisible, viewNotesObj, detailObj, detailRow, handleViewNotes, handleOpenDetail } = useViewNotes();

const { revokeStorageVisible, revokeStorageObj, handleOpenRevokeStorage } = useRevokeStorage();
const { reportVisible, reportObj, reportRow, handleOpenReport } = useAcceptanceReport();
const { applyVisible, applyObj, handleOpenApply, handleRevokeApply } = useApplyForReconciliation();


const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const exportPurchaseList = () => {
    proxy?.download('/system/purchaseInRecord/export', { ...queryParams.value }, `已采购订单${getFormattedTimestamp()}.xlsx`);
};


const { queryParams } = toRefs(data);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);

const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

const getList = async () => {
    try {
        const { code, data } = await getPurchasedOrdersListApi(queryParams.value)
        if (code == 200) {
            dataList.value = data.records;
            totalData.value = data.total;
        }
    } catch (error) {

    }
};

onMounted(() => {
    getList();
});

</script>
<style lang="scss" scoped>
.red {
    color: red;
}

.orange {
    color: orange;
}

.green {
    color: green;
}

.purple {
    color: #DF9BF9;
}
</style>